package com.shipwe.loans.pro

import android.view.View
import com.shipwe.loans.MyApps
import com.shipwe.loans.base.BaseActivity
import com.shipwe.loans.http.HttpHelper
import com.shipwe.loans.http.IHttpCallback
import com.shipwe.loans.utils.dialog.CustomerDialog
import com.shipwe.loans.utils.dialog.TipsDialog
import com.shipwe.loans.utils.event.AppEventData
import com.shipwe.loans.utils.event.AppEventPointHelper
import com.shipwe.loans.utils.event.AppEventSubscriber
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/20 10:32
 * @Annotation：
 */
abstract class ShipweActivity: BaseActivity(), AppEventSubscriber,IShipweView {

    var errorDialog: TipsDialog? = null

    override fun getAct(): ShipweActivity {
        return this
    }

    override fun onStart() {
        super.onStart()
        MyApps.app.publisher.register(this)
    }

    /**
     * 错误提示
     */
    fun showErrorTipsDialog(tips: String,eventRemark: String = ""){
        if (errorDialog == null){
            errorDialog = TipsDialog(this,eventRemark,object : TipsDialog.IDialogCallback{
                override fun onClick(view: View?) {

                }
            })
        }
        if (errorDialog!!.isShowing){
            return
        }
        errorDialog!!.show()
        errorDialog!!.isDoubleButton(false)
        errorDialog!!.setTipsText(tips)
    }

    /**
     * 客服
     */
    fun showCustomerDialog(eventRemark: String = "",pName: String = "", pToken: String = ""){
        CustomerDialog(this)
            .setCustomerCancelable(true)
            .setCustomerCanceledOnTouchOutside(true)
            .setCustomerProductName(pName)
            .setCustomerProductToken(pToken)
            .setCustomerEventRemark(eventRemark)
            .setCustomerClickCallback(object : CustomerDialog.IDialogCallback{
                override fun onClick(view: View?) {
                }
            })
            .show()
    }

    /**
     * post
     */
    fun doPostAction(httpUrl: String,params: JSONObject,callback: IHttpCallback){
        HttpHelper.post(httpUrl,params,this,callback)
    }

    /**
     * 发送事件通知
     */
    fun publishEventData(eventData: AppEventData){
        MyApps.app.publisher.publish(eventData)
    }

    /**
     * 处理订阅事件
     */
    override fun onShipwePublishEvent(event: AppEventData) {
    }

    /**
     * 添加埋点
     */
    fun appEventPointRequestAction(eventName: String, eventRemark: String = "") {
        AppEventPointHelper.appEventPointRequestAction(eventName,eventRemark)
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.cancelTag(this)
        MyApps.app.publisher.detach(this)
    }
}